﻿// GoHome.cpp: 实现文件
//

#include "pch.h"
#include "LiWi.h"
#include "afxdialogex.h"
#include "GoHome.h"
#include "liwishell.h"
#include "Data.h"


// GoHome 对话框

IMPLEMENT_DYNAMIC(GoHome, CDialogEx)

GoHome::GoHome(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_GoHome, pParent)
{

}

GoHome::~GoHome()
{
}

void GoHome::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(GoHome, CDialogEx)
	ON_BN_CLICKED(IDC_Start, &GoHome::OnBnClickedStart)
	ON_CBN_SELCHANGE(IDC_ModeSelect, &GoHome::GetMode)
END_MESSAGE_MAP()


// GoHome 消息处理程序


BOOL GoHome::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CString strText;
	GetDlgItem(IDC_ModeSay0)->SetWindowTextW(_T("模式0：清除所有轴的错误状态(清除AXISSTATUS的找原点中状态)\r\n"));
	GetDlgItem(IDC_ModeSay1)->SetWindowTextW(_T("模式1：轴以爬行速度往正方向回原\r\n"));
	GetDlgItem(IDC_ModeSay2)->SetWindowTextW(_T("模式2：轴以爬行速度往负方向回原\r\n"));
	GetDlgItem(IDC_ModeSay3)->SetWindowTextW(_T("模式3：轴以速度正常速度正方向移动，碰到原点然后以爬行速度反向运动直至离开原点\r\n"));
	GetDlgItem(IDC_ModeSay4)->SetWindowTextW(_T("模式4：轴以速度正常速度反方向移动，碰到原点然后以爬行速度正向运动直至离开原点\r\n"));
	GetDlgItem(IDC_ModeSay5)->SetWindowTextW(_T("模式5：轴以正常速度正方向移动，直至碰到原点IO，然后以爬行速度离开原点，直至碰到负限位\r\n"));
	GetDlgItem(IDC_ModeSay6)->SetWindowTextW(_T("模式6：轴以正常速度反方向移动，直至碰到原点IO，然后以爬行速度离开原点，直至碰到正限位\r\n"));
	GetDlgItem(IDC_ModeSay7)->SetWindowTextW(_T("模式8：轴以正常速度正方向移动，直至碰到原点IO\r\n"));
	GetDlgItem(IDC_ModeSay8)->SetWindowTextW(_T("模式9：轴以正常速度反方向移动，直至碰到原点IO\r\n"));
	GetDlgItem(IDC_ModeSay9)->SetWindowTextW(_T("模式21：使用Ethercat驱动器回零功能，此时模式2有效"));

	((CComboBox*)GetDlgItem(IDC_ModeSelect))->InsertString(0, _T("0"));
	((CComboBox*)GetDlgItem(IDC_ModeSelect))->InsertString(1, _T("1"));
	((CComboBox*)GetDlgItem(IDC_ModeSelect))->InsertString(2, _T("2"));
	((CComboBox*)GetDlgItem(IDC_ModeSelect))->InsertString(3, _T("3"));
	((CComboBox*)GetDlgItem(IDC_ModeSelect))->InsertString(4, _T("4"));
	((CComboBox*)GetDlgItem(IDC_ModeSelect))->InsertString(5, _T("5"));
	((CComboBox*)GetDlgItem(IDC_ModeSelect))->InsertString(6, _T("6"));
	((CComboBox*)GetDlgItem(IDC_ModeSelect))->InsertString(7, _T("8"));
	((CComboBox*)GetDlgItem(IDC_ModeSelect))->InsertString(8, _T("9"));
	((CComboBox*)GetDlgItem(IDC_ModeSelect))->InsertString(9, _T("21"));
	((CComboBox*)GetDlgItem(IDC_ModeSelect))->SetCurSel(1);



	((CButton*)GetDlgItem(IDC_GoBack))->SetCheck(true);

	//回原参数
	((CButton*)GetDlgItem(IDC_Open0))->SetCheck(false);
	((CButton*)GetDlgItem(IDC_Open1))->SetCheck(false);
	((CButton*)GetDlgItem(IDC_Open2))->SetCheck(false);
	((CButton*)GetDlgItem(IDC_Open3))->SetCheck(false);

	GetDlgItem(IDC_FwdIO0)->SetWindowTextW(_T("-1"));
	GetDlgItem(IDC_FwdIO1)->SetWindowTextW(_T("-1"));
	GetDlgItem(IDC_FwdIO2)->SetWindowTextW(_T("-1"));
	GetDlgItem(IDC_FwdIO3)->SetWindowTextW(_T("-1"));

	GetDlgItem(IDC_RevIO0)->SetWindowTextW(_T("-1"));
	GetDlgItem(IDC_RevIO1)->SetWindowTextW(_T("-1"));
	GetDlgItem(IDC_RevIO2)->SetWindowTextW(_T("-1"));
	GetDlgItem(IDC_RevIO3)->SetWindowTextW(_T("-1"));

	GetDlgItem(IDC_HomeIO0)->SetWindowTextW(_T("-1"));
	GetDlgItem(IDC_HomeIO1)->SetWindowTextW(_T("-1"));
	GetDlgItem(IDC_HomeIO2)->SetWindowTextW(_T("-1"));
	GetDlgItem(IDC_HomeIO3)->SetWindowTextW(_T("-1"));

	GetDlgItem(IDC_WaitCT0)->SetWindowTextW(_T("0"));
	GetDlgItem(IDC_WaitCT1)->SetWindowTextW(_T("0"));
	GetDlgItem(IDC_WaitCT2)->SetWindowTextW(_T("0"));
	GetDlgItem(IDC_WaitCT3)->SetWindowTextW(_T("0"));

	GetDlgItem(IDC_PianYi0)->SetWindowTextW(_T("0"));
	GetDlgItem(IDC_PianYi1)->SetWindowTextW(_T("0"));
	GetDlgItem(IDC_PianYi2)->SetWindowTextW(_T("0"));
	GetDlgItem(IDC_PianYi3)->SetWindowTextW(_T("0"));

	GetDlgItem(IDC_Creep0)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Creep1)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Creep2)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Creep3)->SetWindowTextW(_T("100"));

	//初始参数
	GetDlgItem(IDC_Speed_5)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Units_5)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Accel_5)->SetWindowTextW(_T("1000"));
	GetDlgItem(IDC_Decel_5)->SetWindowTextW(_T("1000"));



	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

void GoHome::GetMode()
{
	//int nIndex = m_comboBox.GetCurSel();

}


void GoHome::OnBnClickedStart()
{
	// TODO: 在此添加控件通知处理程序代码
	int enable = 0;	//轴使能标志
	CString speedValue;
	CString unitsValue;
	CString accelValue;
	CString decelValue;
	for (int var = 0; var < 4; var++)
	{
		if (((CButton*)GetDlgItem(IDC_Open0 + var))->GetCheck())
		{
			PMC_GetAxisEnable(Data::handle, var, &enable);
			if (enable == 0)
			{
				MessageBox(_T("请先对需要回原的轴使能"));
				return;
			}
			GetDlgItem(IDC_Speed_5)->GetWindowTextW(speedValue);
			GetDlgItem(IDC_Units_5)->GetWindowTextW(unitsValue);
			GetDlgItem(IDC_Accel_5)->GetWindowTextW(accelValue);
			GetDlgItem(IDC_Decel_5)->GetWindowTextW(decelValue);
			PMC_SetSpeed(Data::handle, var, _ttof(speedValue));
			PMC_SetUnits(Data::handle, var, _ttof(unitsValue));
			PMC_SetAccel(Data::handle, var, _ttof(accelValue));
			PMC_SetDecel(Data::handle, var, _ttof(decelValue));

			//获取模式
			int nIndex = ((CComboBox*)GetDlgItem(IDC_ModeSelect))->GetCurSel();
			CString selectmode;
			((CComboBox*)GetDlgItem(IDC_ModeSelect))->GetLBText(nIndex, selectmode);
			int model = ((CButton*)GetDlgItem(IDC_GoBack))->GetCheck() ? _ttoi(selectmode) + 10 : _ttoi(selectmode);

			//设置回原参数
			//正限位
			CString fwdIO;
			GetDlgItem(IDC_FwdIO0 + var)->GetWindowTextW(fwdIO);
			PMC_SetFwdIn(Data::handle, var, _ttof(fwdIO));
			//负限位
			CString revIO;
			GetDlgItem(IDC_RevIO0 + var)->GetWindowTextW(revIO);
			PMC_SetRevIn(Data::handle, var, _ttof(revIO));
			//原点
			CString homeIO;
			GetDlgItem(IDC_HomeIO0 + var)->GetWindowTextW(homeIO);
			PMC_SetDatumIn(Data::handle, var, _ttof(homeIO));
			//反找等待CT
			CString waitCT;
			GetDlgItem(IDC_WaitCT0 + var)->GetWindowTextW(waitCT);
			PMC_SetHomeWait(Data::handle, var, _ttof(waitCT));
			//偏移距离
			CString pianYi;
			GetDlgItem(IDC_PianYi0 + var)->GetWindowTextW(pianYi);
			//PMC_BusCmd_SetDatumOffpos(Data::handle, var, _ttof(pianYi));
			//爬行速度
			CString creep;
			GetDlgItem(IDC_Creep0 + var)->GetWindowTextW(creep);
			PMC_SetCreep(Data::handle, var, _ttof(creep));
			//回原
			PMC_Single_Datum(Data::handle, var, model);
		}
	}

}
